<?php

namespace App\Http\Controllers\Admin\Finance\Receivable;

use App\Http\Controllers\Controller;
use App\Http\Requests\airline\base\BanRequest;
use App\Models\DictionaryModel;
use App\Models\finance\PayableTmpDetailModel;
use App\Models\finance\PayableTmpModel;
use App\Models\finance\ReceivableTmpDetailModel;
use App\Models\finance\ReceivableTmpModel;
use App\Models\OrderModel;
use App\Models\TakeOrderModel;
use App\Http\Requests\finance\TmpRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Template extends Controller
{
    use TmpRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $query = ReceivableTmpModel::query()->with(['create_admin', 'detail']);

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'name' => $item->name,
                'detail' => $item->detail,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }


    public function add(Request $request) {
        $auth = $request->get('auth');
        $name = $request->input('name',  '');
        $detail = $request->input('detail', []);

        $check_res = $this->add_check2($request->all());
        if($check_res->faild){
            return $this->error($check_res->err);
        }


        $find = ReceivableTmpModel::query()->where('name', $name)->first();
        if($find) {
            return $this->error('模板名称已存在');
        }

        if(!$detail || !is_array($detail)){
            return $this->error('费用明细不能为空');
        }

        DB::beginTransaction();
        try{
            $tmp = new ReceivableTmpModel();
            $tmp->name = $name;
            $tmp->created_admin_id = $auth->id;
            $tmp->save();

            foreach ($detail as $item){
                $tmp_detail = new ReceivableTmpDetailModel();
                $tmp_detail->tmp_id = $tmp->id;
                $tmp_detail->name = $item['name'];
                $tmp_detail->number = $item['number'];
                $tmp_detail->unit = $item['unit'];
                $tmp_detail->price = $item['price'];
                $tmp_detail->currency_id = $item['currency_id'];
                $tmp_detail->save();
            }

            DB::commit();
        } catch(\Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();

    }

    public function update(Request $request, $id) {

        $name = $request->input('name',  '');
        $detail = $request->input('detail', []);
        $check_res = $this->add_check2($request->all());
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $find = ReceivableTmpModel::query()->where('id', $id)->first();
        if(!$find) {
            return $this->error('模板不存在');
        }

        DB::beginTransaction();
        try{
            $find->name = $name;
            $find->save();
            ReceivableTmpDetailModel::query()->where('tmp_id', $id)->delete();
            foreach ($detail as $item){
                $tmp_detail = new ReceivableTmpDetailModel();
                $tmp_detail->tmp_id = $id;
                $tmp_detail->name = $item['name'];
                $tmp_detail->agent_id = $item['agent_id'];
                $tmp_detail->number = $item['number'];
                $tmp_detail->unit = $item['unit'];
                $tmp_detail->price = $item['price'];
                $tmp_detail->currency_id = $item['currency_id'];
                $tmp_detail->save();
            }

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error($e->getMessage());
        }

        return $this->success();
    }

    public function delete(Request $request, $id) {

        $find = ReceivableTmpModel::query()->where('id', $id)->first();
        if(!$find) {
            return $this->error('模板不存在');
        }
        $find->delete();

        ReceivableTmpDetailModel::query()->where('tmp_id', $id)->delete();
        return $this->success();
    }




}
